/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-6-14 下午3:52
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.jdmetro.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dqhl.jdmetro.JDMetroApplication;
import com.example.dqhl.jdmetro.R;
import com.example.dqhl.jdmetro.bean.User;
import com.example.dqhl.jdmetro.utils.UserSaveUtil;
import com.example.dqhl.jdmetro.widget.CustomProgressDialog;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoImpl;
import com.jph.takephoto.model.InvokeParam;
import com.jph.takephoto.model.TContextWrap;
import com.jph.takephoto.model.TResult;
import com.jph.takephoto.permission.InvokeListener;
import com.jph.takephoto.permission.PermissionManager;
import com.jph.takephoto.permission.TakePhotoInvocationHandler;

/**
 * description:BaseActivity
 * autour:guanluocang
 * date:17/6/14 16:22
 */
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener, TakePhoto.TakeResultListener, InvokeListener {

    private Context context;
    private InputMethodManager mInputMethodManager;
    private CustomProgressDialog mCustomProgressDialog;
    protected LocalBroadcastManager localBroadcastManager;

    protected JDMetroApplication app;
    private ImageView iv_top_back;
    private TextView tv_top_title;
    private TextView tv_top_msg;
    protected User user;

    private static final String TAG = BaseActivity.class.getName();
    private TakePhoto takePhoto;
    private InvokeParam invokeParam;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        app = (JDMetroApplication) getApplicationContext();
        context = this;
        user = (User) UserSaveUtil.readUser(this);
        localBroadcastManager = LocalBroadcastManager.getInstance(this);

        mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        initView(savedInstanceState);
        initData();
        initListener();
        getTakePhoto().onCreate(savedInstanceState);
    }


    public abstract void initView(Bundle savedInstanceState);

    public abstract void initData();

    public abstract void initListener();

    @Override
    public void onClick(View v) {

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        getTakePhoto().onSaveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        getTakePhoto().onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.TPermissionType type = PermissionManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.handlePermissionsResult(this, type, invokeParam, this);
    }

    /**
     * 获取TakePhoto实例
     *
     * @return
     */
    public TakePhoto getTakePhoto() {
        if (takePhoto == null) {
            takePhoto = (TakePhoto) TakePhotoInvocationHandler.of(this).bind(new TakePhotoImpl(this, this));
        }
        return takePhoto;
    }

    @Override
    public void takeSuccess(TResult result) {
        Log.i(TAG, "takeSuccess：" + result.getImage().getCompressPath());
    }

    @Override
    public void takeFail(TResult result, String msg) {
        Log.i(TAG, "takeFail:" + msg);
    }

    @Override
    public void takeCancel() {
        Log.i(TAG, getResources().getString(com.jph.takephoto.R.string.msg_operation_canceled));
    }

    @Override
    public PermissionManager.TPermissionType invoke(InvokeParam invokeParam) {
        PermissionManager.TPermissionType type = PermissionManager.checkPermission(TContextWrap.of(this), invokeParam.getMethod());
        if (PermissionManager.TPermissionType.WAIT.equals(type)) {
            this.invokeParam = invokeParam;
        }
        return type;
    }

    public void initTitleBar(String title) {
        iv_top_back = (ImageView) findViewById(R.id.iv_top_back);
        iv_top_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_top_title = (TextView) findViewById(R.id.tv_top_title);
        tv_top_title.setText(title);
        tv_top_msg = (TextView) findViewById(R.id.tv_top_msg);
    }

    public void initTitleBar(int resId) {
        String title = getResources().getString(resId);
        iv_top_back = (ImageView) findViewById(R.id.iv_top_back);
        iv_top_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_top_title = (TextView) findViewById(R.id.tv_top_title);
        tv_top_title.setText(title);
        tv_top_msg = (TextView) findViewById(R.id.tv_top_msg);
    }

    public void setLeftBackVisibility(int visible) {
        if (iv_top_back != null) {
            iv_top_back.setVisibility(visible);
        } else {
            iv_top_back = (ImageView) findViewById(R.id.iv_top_back);
            iv_top_back.setVisibility(visible);
        }
    }

    public void setRightTextVisibility(int visible) {
        if (tv_top_msg != null) {
            tv_top_msg.setVisibility(visible);
        } else {
            tv_top_msg = (TextView) findViewById(R.id.tv_top_msg);
            tv_top_msg.setVisibility(visible);
        }
    }

    /**
     * 显示自定义ProgressBar，提示用户操作正在处理中
     *
     * @param content:要显示的提示内容包括ProgressBar和text
     */
    public void showLoadingBar(String content) {
        if (mCustomProgressDialog == null) {
            mCustomProgressDialog = new CustomProgressDialog(this, content);
        } else {
            mCustomProgressDialog.setContent(content);
        }
        mCustomProgressDialog.show();
    }

    /**
     * 释放自定义ProgressBar，提示操作结果
     */
    public void hideLoadingBar() {
        if (mCustomProgressDialog != null && mCustomProgressDialog.isShowing()) {
            mCustomProgressDialog.cancel();
        }
    }

    /**
     * 点击空白处隐藏软键盘
     *
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
                mInputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                        mInputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        return super.onTouchEvent(event);
    }
}
